package com.ncs.cns.business.service;

import java.util.List;

import android.content.Context;

import com.ncs.cns.business.dao.impl.UserSessionDAO;
import com.ncs.cns.business.to.UserSessionInfo;
import com.ncs.cns.business.util.DateUtil;

public class UserSessionService {

	private UserSessionDAO sessionDAO;

	public UserSessionService(Context context) {
		sessionDAO = new UserSessionDAO(context);
	}

	public UserSessionInfo findByUserId(String userId) {
		UserSessionInfo info = null;
		List<UserSessionInfo> list = (List<UserSessionInfo>) sessionDAO.findBy(
				"USER_ID", userId);
		if (list != null && list.size() > 0) {
			info = list.get(0);
		}
		return info;
	}

	public boolean existUser(String userId) {
		return findByUserId(userId) != null;
	}

	public String getLastRefreshTime(String userId) {
		UserSessionInfo info = findByUserId(userId);
		String lastTime = "";
		if (info != null) {
			lastTime = info.getLastRefreshTime();
		}
		return lastTime;
	}

	public void updateLastRefreshTime(String userId) {
		UserSessionInfo info = findByUserId(userId);
		info.setLastRefreshTime(DateUtil.getCurrentDateStr());
		sessionDAO.update(info);
	}

	public void updateSession(UserSessionInfo info) {
		sessionDAO.update(info);
	}
}
